<?php
namespace app\index\controller;
use app\index\controller\Base;
use app\index\model\User as userModel;
use app\index\model\Option;
use app\index\model\Confirm;
use think\Db;
class User extends Base
{
    public function index($uid = 0)
        {
        $user = new userModel;
        if($uid == 0 && empty(session('uid')))
                {
            return $this->error('用户不存在！','index\index\index');
        } else {
            if($uid == 0)
                        {
                $uid = session('uid');
            }
            //获取用户帖子信息
            $userTopicList = Db::name('topic')->where('uid',$uid)->select();
            if(empty($userTopicList))
                        {
                return $this->error('用户不存在。');
            }
            foreach ($userTopicList as $key => $value) {
                $value['content'] = strip_tags(htmlspecialchars_decode($value['content']));
                $value['time_format'] = time_format($value['create_time']);
                $value['userData'] =Db::name('user')->where('uid',$value['uid'])->field('username,avatar')->find();
                $userTopicList[$key] = $value;
            }
            if(session('uid') == $uid)
                        {
                return view('index',[
                                'option' => $this->siteOption('用户信息'),
                                'userData' => userModel::get($uid),
                                'userTopic' => $userTopicList,
                                ]);
            }
            return view('index_public',[
                        'option' => $this->siteOption('用户信息'),
                        'userData' => userModel::get(session('uid')),
                        'userInfo' => $userInfo,
                        'userTopic' => $userTopicList,
                        ]);
        }
    }
    public function set()
        {
        if(empty(session('uid')))
                {
            return \redirect('index\user\login');
        }
        if(!empty(input('post.')))
                {
            if(empty(session('salt')))
                        {
                return ['code'=>'-1','message'=>'非法操作！'];
            }
            if(input('post.type') == 'pass')
                        {
                $user = userModel::get(session('uid'));
                if(password_verify(input('post.password'),$user->password))
                                {
                    return ['code'=>'-1','message'=>'新密码不一致或旧密码不正确。'];
                } elseif (input('password') !== input('repassword')) {
                    return ['code'=>'-1','message'=>'新密码不一致或旧密码不正确。'];
                } else {
                    $user->password = input('password');
                    $user->save();
                    session(null);
                    return $this->success('修改成功！请使用新密码登录。','login');
                    // return ['code'=>'0','message'=>'修改成功！请使用新密码登录。'];
                }
            }
        }
    }
    public function login()
        {
        if(!empty(session('uid')))
                {
            return redirect('index');
        }
        if(!empty(input('post.')))
                {
            $user = UserModel::where('email',input('post.email'))->find();
            if($user == null)
                        {
                return json(['code'=>'-1','message'=>'用户或不存在']);
            } else{
                if(password_verify(input('post.password'),$user->password))
                                {
                    $Confirm = Confirm::getValue($user->uid);
                    if(!empty($Confirm['code'])){
                        return json(['code'=>1,'message'=>'账户未激活，请激活后登陆。']);
                    }
                    session('uid',$user->uid);
                    session('gid',$user->gid);
                    session('username',$user->username);
                    Db::name('user')->where('uid',$user->uid)->setInc('logins');
                    //增加登录次数值
                    return json(['code'=>'0','message'=>'登录成功！欢迎回来……','url'=>url('index\user\index')]);
                } else{
                    return ['code'=>'-1','message'=>'用户名或密码错误！'];
                }
            }
        }
        return view('login',[
                    'option' => $this->siteOption('登录'),
                ]);
    }
    public function reg($step = 1)
        {
        if(!empty(session('uid')))
                {
            return redirect('index');
        }
        if(!empty(input('post.')))
        {

            $res = $this->validate(input('post.email'),'app\inde\validate\User.valiEmail');
            if($res !== true)
                            {
                return json(['code'=>'-1','message'=>$result]);
            }
            $res = UserModel::where('email',input('post.email'))->find();
            if(!empty($res))
                            {
                return json(['code'=>'-1','message'=>'该邮箱已注册']);
            }
            //调用Mailmodel，发送邮件
            $email = model("Mail");
            $arry = [
                                '{siteTitle}' => Option::getValue('siteTitle'),
                                '{userName}' => $userData['username'],
                                '{code}' => createStr(6),
                            ];
            $title = Option::getValue('reg_mail_title');
            $content = Option::getValue('reg_mail_content');
            $title = strtr($title,$arry);
            $content = strtr($content,$arry);
            $email->send(input('post.mail'),input('post.mail'),$title,$content);
            return json(['code'=>0,'message'=>'我们给你发送了一封邮件，请凭借验证码注册','url'=>url('index\user\login')]);
        }
        
        return view('reg',[
                    'option' => $this->siteOption('注册'),
                ]);
    }
    public function logout()
        {
        if(!empty(session('uid')))
                {
            session(null);
            return $this->success('退出成功！','index/index/index');
        } else {
            return $this->error('当前无需退出呢！');
        }
    }
    public function activity($uid,$code)
        {
        $data = Confirm::getValue($uid);
        if($data['time'] < time())
                {
            Confirm::setConfirm($uid);
            $code = createStr(6);
            Confirm::create([
                                'uid' => $uid,
                                'code' => $code,
                                'time' => time() + 300000,
                                'type' => 'register',
                            ]);
            $user = userModel::get($uid);
            //调用Mailmodel，发送邮件
            $email = model("Mail");
            $arry = [
                            '{siteTitle}' => Option::getValue('siteTitle'),
                            '{userName}' => $userData['username'],
                            '{url}' => $_SERVER['SERVER_NAME'].url('index/User/activity',['uid'=>$uid,'code'=>$code]),
                        ];
            $title = Option::getValue('reg_mail_title');
            $content = Option::getValue('reg_mail_content');
            $title = strtr($title,$arry);
            $content = strtr($content,$arry);
            $email->send($user->email,$user->username,$title,$content);
            return $this->error('链接已经过期，已经重新发送一封邮件。');
        } elseif($data['code'] === $code){
            Confirm::setConfirm($uid);
            return $this->success('账户成功激活！','index/User/login');
        }
    }
}